package com.ltu.timkor6.gamesite.shared;

import java.util.ArrayList;
import java.util.List;

public class DoubleArray
{
	private int rows;
	private int columns;
	
	private int currentRow = 0;
	private int currentCol = 0;
	
	private List<List<Integer>> array;
	
	public DoubleArray(int rows, int columns)
	{
		this.rows = rows;
		this.columns = columns;
		
		// Initialize the rows
		this.array = new ArrayList<List<Integer>>(rows);
		
		// Initialize the columns
		List<Integer> list;
		for(int row = 0; row < rows; row++)
		{
			list = new ArrayList<Integer>(this.columns);
			
			for(int column = 0; column < columns; column++)
			{
				list.add(0);
			}
			
			this.array.add(list);
		}
	}
	
	public DoubleArray(List<List<Integer>> list)
	{
		this.array = list;
	}
	
	public List<List<Integer>> get()
	{
		return this.array;
	}

	public List<Integer> get(int row)
	{
		return this.array.get(row);
	}
	
	public Integer get(int row, int column)
	{
		return get(row).get(column);
	}
	
	public void add(int value)
	{
		get(this.currentRow).set(currentCol, value);
		
		if(this.currentCol == this.columns-1)
		{
			this.currentCol = 0;
			this.currentRow++;
		}
		else
		{
			this.currentCol++;
		}
	}
	
	public void set(int row, List<Integer> column)
	{
		this.array.set(row, column);
	}
	
	public void set(int row, int column, int value)
	{
		get(row).set(column, value);
	}

	/**
	 * Size of the column span
	 * @param row
	 * @return
	 */
	public int getRowSize()
	{
		return this.rows;
	}
	
	/**
	 * Size of the row span
	 * @return
	 */
	public int getColSize()
	{
		return this.columns;
	}

	public void set(int position, int value)
	{
		int row = position / this.columns;
		int column = position % this.columns;
		
		set(row, column, value);
	}
}
